﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Weazel.Math;

#endregion

namespace Weazel.Math.Test
{
  [TestClass]
  public class TestGetRange
  {
    [TestMethod]
    public void TestGetRangeWrongIndices()
    {
      Integer a = new Integer(32, 0xAAAAAAAA, false);
      Integer r = new Integer(32, false);

      try
      {
        Integer.GetRange(ref r, a, 4, 0);

        // Exception expected
        Assert.Fail();
      }
      catch (IndexOutOfRangeException)
      {       
      }
    }

    [TestMethod]
    public void TestGetRangeIndex1OutOfRange()
    {
      Integer a = new Integer(32, 0xAAAAAAAA, false);
      Integer r = new Integer(32, false);

      try
      {
        Integer.GetRange(ref r, a, 34, 100);

        // Exception expected
        Assert.Fail();
      }
      catch (IndexOutOfRangeException)
      {
      }
    }

    [TestMethod]
    public void TestGetRangeIndex2OutOfRange()
    {
      Integer a = new Integer(32, 0xAAAAAAAA, false);
      Integer r = new Integer(32, false);

      try
      {
        Integer.GetRange(ref r, a, 20, 100);

        // Exception expected
        Assert.Fail();
      }
      catch (IndexOutOfRangeException)
      {
      }
    }

    [TestMethod]
    public void TestGetRangeResultTooSmall()
    {
      Integer a = new Integer(32, 0xAAAAAAAA, false);
      Integer r = new Integer(8, false);

      try
      {
        Integer.GetRange(ref r, a, 0, 32);

        // Exception expected
        Assert.Fail();
      }
      catch (IndexOutOfRangeException)
      {
      }
    }

    [TestMethod]
    public void TestGetRange32Bits1()
    {
      Integer a = new Integer(32, 0x10, false);
      Integer r = new Integer(8, false);

      Integer.GetRange(ref r, a, 0, 7);

      Assert.AreEqual("10", r.ToHexadecimalString());
      Assert.AreEqual(8, r.Width);
    }

    [TestMethod]
    public void TestGetRange32Bits2()
    {
      Integer a = new Integer(32, 0x1FF, false);
      Integer r = new Integer(8, false);

      Integer.GetRange(ref r, a, 0, 7);

      Assert.AreEqual("ff", r.ToHexadecimalString());
      Assert.AreEqual(8, r.Width);
    }

    [TestMethod]
    public void TestGetRange64Bits1()
    {
      Integer a = new Integer(64, 0x1FF, false);
      Integer r = new Integer(8, false);

      Integer.GetRange(ref r, a, 0, 7);

      Assert.AreEqual("ff", r.ToHexadecimalString());
    }

    [TestMethod]
    public void TestGetRange64Bits2()
    {
      Integer a = new Integer(64, "0x10FFFFFFFF", false);
      Integer r = new Integer(8, false);

      Integer.GetRange(ref r, a, 32, 39);

      Assert.AreEqual("10", r.ToHexadecimalString());
      Assert.AreEqual(8, r.Width);
    }


    [TestMethod]
    public void TestGetRangeAcrossWordBoundry()
    {
      Integer a = new Integer(64, "0xBCFFFFFFF", false);
      Integer r = new Integer(8, false);

      Integer.GetRange(ref r, a, 28, 35);

      Assert.AreEqual("bc", r.ToHexadecimalString());
      Assert.AreEqual(8, r.Width);
    }

    [TestMethod]
    public void TestGetRangeResultTooNarrow()
    {
      Integer a = new Integer(64, "0xBCFFFFFFF", false);
      Integer r = new Integer(8, false);

      try
      {
        Integer.GetRange(ref r, a, 28, 40);
        Assert.Fail();
      }
      catch (IndexOutOfRangeException)
      {
        return; // success
      }

      Assert.Fail();
    }
  }
}
